package de.westranger.forex.trading.analysis;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class SREvaluationPlot {

	public static void main(String[] args) throws IOException {
		final BufferedReader br = new BufferedReader(new FileReader("log.txt"));
		Map<Double,List<Double>> profit = new TreeMap<Double,List<Double>>();
		Map<Double,List<Double>> posTrade = new TreeMap<Double,List<Double>>();
		Map<Double,List<Double>> negTrade = new TreeMap<Double,List<Double>>();
		final int param = 1;

		while(br.ready()){
			final double[] line = parseLine(br.readLine(),"\t");
			//final double value = line[param];
			final double value = Math.round(line[0]/line[param]);

			List<Double> list;
			if(profit.containsKey(value)){
				list = profit.remove(value);
			}else{
				list = new LinkedList<Double>();
			}
			list.add(line[7]);
			profit.put(value, list);
			
			final double sum = line[5] + line[6]; 			
			if(posTrade.containsKey(value)){
				list = posTrade.remove(value);
			}else{
				list = new LinkedList<Double>();
			}
			list.add(sum > 0.0 ? (line[5] / sum)*100.0 : 0.0);
			posTrade.put(value, list);			
			
			if(negTrade.containsKey(value)){
				list = negTrade.remove(value);
			}else{
				list = new LinkedList<Double>();
			}
			list.add(sum > 0.0 ? (line[6] / sum)*100.0 : 0.0);
			negTrade.put(value, list);
		}

		plot(profit,"profit");
		plot(negTrade,"negTrade");
		plot(posTrade,"posTrade");

		br.close();
	}
	
	private static double[] parseLine(final String line,final String delim){
		final List<Double> list = new ArrayList<Double>();
		final StringTokenizer tk = new StringTokenizer(line,delim);
		
		while(tk.hasMoreTokens()){
			list.add(Double.parseDouble(tk.nextToken()));
		}
		
		final double[] result = new double[list.size()];
		for(int i=0;i<list.size();i++){
			result[i] = list.get(i);
		}
		return result;
	}

	private static double mean(final List<Double> list){
		double result = 0.0;
		for(double value:list){
			result += value;
		}
		return result / list.size();
	}
	
	private static double std(final List<Double> list){
		double result = 0.0;
		for(double value:list){
			result += value;
		}
		result /= list.size();
		double sum = 0.0;
		for(double value:list){
			sum += (value - result)*(value - result);
		}
		sum /= list.size();
		return Math.sqrt(sum);
	}
	
	private static double median(final List<Double> list){
		Collections.sort(list,new Comparator<Double>() {
		    @Override
		    public int compare(Double d1, Double d2) {
		        return  d1.compareTo(d2);
		    }
		});
		return list.get(list.size()/2);
	}
	
	
	private static void plot(final Map<Double,List<Double>> map,final String name){
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		for(double key:map.keySet()){
			final double mean = mean(map.get(key));
			final double median = median(map.get(key));
			final double std = std(map.get(key));

			dataset.setValue(mean, "mean", Double.toString(key));
			dataset.setValue(median, "median", Double.toString(key));
			dataset.setValue(std, "stdev", Double.toString(key));
		}

		final JFreeChart chart = ChartFactory.createBarChart(name, "", "", dataset, PlotOrientation.VERTICAL, false, true, false);

		try {
			ChartUtilities.saveChartAsJPEG(new File("./out/plot_eval_"+name+".jpg"), chart, 1200, 768);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
}
